//
//  BasicView.m
//  WeiBo
//
//  Created by 小小罗 on 15/11/24.
//  Copyright © 2015年 RealMadrid. All rights reserved.
//

#import "BasicView.h"



@interface BasicView ()
//图标
@property(nonatomic,strong) UIImageView*iconView;
//遮罩图像
@property(nonatomic,strong) UIImageView*maskIconView;
//小房子
@property(nonatomic,strong) UIImageView*homeIconView;
//消息文字
@property(nonatomic,strong) UILabel*messageLabel;
//注册按钮
@property(nonatomic,strong) UIButton*registerButton;
//登录按钮
@property(nonatomic,strong) UIButton*loginButton;

@end


@implementation BasicView
#pragma mark - /****************** 懒加载 ******************/
//图标
- (UIImageView *)iconView{
    
    if(_iconView == nil){
    
    _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
        
    }
    
    return _iconView;
}

//遮罩图像
- (UIImageView *)maskIconView{
    
    if (_maskIconView == nil) {
        
        _maskIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
        
    }
    
    return _maskIconView;
}

//小房子
- (UIImageView *)homeIconView{
   
    if (_homeIconView == nil) {
        
       _homeIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
        
    }
    return _homeIconView;
}

//消息文字
- (UILabel *)messageLabel{
    
    if (_messageLabel == nil) {
        
        _messageLabel = [UILabel initWithTitle:@"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜" fontSize:14 color:[UIColor darkGrayColor] screenInset:0];

    }
    
    return _messageLabel;
}

//注册按钮
- (UIButton *)registerButton{
    
    if (_registerButton == nil) {
        
        _registerButton = [UIButton initWithTitle:@"注册" color:[UIColor orangeColor] backImageName:@"common_button_white_disable"];
        
    }
    
    return _registerButton;
}

//登录按钮
- (UIButton *)loginButton{
    
    if (_loginButton == nil) {
        
        _loginButton = [UIButton initWithTitle:@"登录" color:[UIColor orangeColor] backImageName:@"common_button_white_disable"];
        
    }
    
    return _loginButton;
}

#pragma mark ****************** 添加子控件 ******************
- (void)addChildView{
    
    //添加子控件
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeIconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    //设置自动布局
    // 1> 图标
    // make 理解为要添加的约束对象
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).offset(-60);
    }];
    
    // 2> 小房子
    [self.homeIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.centerY.equalTo(self.iconView.mas_centerY);
    }];
    
    // 3> 消息文字
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.top.equalTo(self.iconView.mas_bottom).offset(16);
        make.width.mas_equalTo(224);
        make.height.mas_equalTo(36);
    }];
    
    // 4> 注册按钮
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.messageLabel.mas_left);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(16);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(36);
    }];
    
    // 5> 登录按钮
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.messageLabel.mas_right);
        make.top.equalTo(self.registerButton.mas_top);
        make.width.equalTo(self.registerButton.mas_width);
        make.height.equalTo(self.registerButton.mas_height);
    }];
    
    // 6. 遮罩图像
    [self.maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.registerButton.mas_bottom);
    }];
    
    // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
    
}


- (void)setUpInfoWithImageName:(NSString *)imageName title:(NSString *)title
{
    self.messageLabel.text = title;
    //如果图片名称为nil  那么说明是首页
    
    if (imageName != nil) {
        self.iconView.image = [UIImage imageNamed:imageName];
        //隐藏小房子
        self.homeIconView.hidden = YES;
        [self sendSubviewToBack:self.maskIconView];
        
        return ;
    }
    
    [self startAnim];

}

//设置首页动画  用核心动画
- (void)startAnim{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = @(2 * M_PI);
    anim.repeatCount = MAXFLOAT;
    anim.duration = 20;
    anim.removedOnCompletion = NO;
    
    [self.iconView.layer addAnimation:anim forKey:nil];

}




@end
